﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Text;
using System.Web;

namespace DotNETX.Web.Handlers
{
    /// <summary>
    /// Web代理，用于解决跨域禁止
    /// 用法
    /// <add verb="*" path="Proxy.ashx" type="DotNETX.Web.Handlers.ProxyHandler, DotNETX.Web" />
    /// </summary>
    public class ProxyHandler : BaseHandler
    {
        public override void ProcessRequest(HttpContext context)
        {
            SetNoCache();
            context.Response.ContentType = "xml/application";
            System.Net.WebClient wc = new System.Net.WebClient();
            string des = context.Request.Url.Query;
            if (des.StartsWith("?")) des = des.Substring(1);
            using (System.IO.Stream s = wc.OpenRead(des))
            {
                byte[] buffer = new byte[1024];
                int count = 0;
                count = s.Read(buffer, 0, 1024);
                while (count > 0)
                {
                    context.Response.OutputStream.Write(buffer, 0, count);
                    count = s.Read(buffer, 0, 1024);
                }
            }
        }
    }
}
